Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@thi.ng/compare
Advanced tools
Comparators with support for types implementing the @thi.ng/api/ICompare interface
@thi.ng/compare is a utility library for various comparison operations in JavaScript. It provides a collection of comparator functions for different data types and use cases, making it easier to perform sorting, equality checks, and other comparison-based operations.
Basic Comparators
Provides basic comparator functions for general use, including ascending and descending numerical comparisons.
const { compare, compareNumAsc, compareNumDesc } = require('@thi.ng/compare');
console.log(compare(5, 10)); // -1
console.log(compareNumAsc(5, 10)); // -1
console.log(compareNumDesc(5, 10)); // 1
String Comparators
Includes specialized comparators for string values, with options for case-sensitive and case-insensitive comparisons.
const { compareStr, compareStrCaseInsensitive } = require('@thi.ng/compare');
console.log(compareStr('apple', 'banana')); // -1
console.log(compareStrCaseInsensitive('Apple', 'apple')); // 0
Array Comparators
Allows for comparison of arrays, element by element, to determine their order or equality.
const { compareArrays } = require('@thi.ng/compare');
console.log(compareArrays([1, 2, 3], [1, 2, 4])); // -1
console.log(compareArrays([1, 2, 3], [1, 2, 3])); // 0
Object Comparators
Provides comparators for objects based on specific keys, useful for sorting arrays of objects.
const { compareByKey } = require('@thi.ng/compare');
const obj1 = { id: 1, name: 'Alice' };
const obj2 = { id: 2, name: 'Bob' };
console.log(compareByKey('id')(obj1, obj2)); // -1
Lodash is a popular utility library that provides a wide range of functions for common programming tasks, including comparison operations. It offers functions like _.isEqual for deep equality checks and _.sortBy for sorting collections. Compared to @thi.ng/compare, Lodash is more comprehensive but also larger in size.
fast-deep-equal is a small and fast library for deep equality checks. It is highly optimized for performance and is often used when deep comparison of objects or arrays is needed. Unlike @thi.ng/compare, it focuses solely on deep equality and does not provide other comparison utilities.
Ramda is a functional programming library for JavaScript that includes a variety of utility functions, including comparison functions like R.equals and R.sort. It emphasizes immutability and functional programming principles. While it offers similar comparison functionalities, it is part of a broader functional programming toolkit.
This project is part of the @thi.ng/umbrella monorepo.
Comparators with optional support for types implementing the
@thi.ng/api
ICompare
interface.
Since v1.2.0 additional higher-order comparators are included, e.g. to reverse the ordering of an existing comparator and allow hierarchical sorting by multiple keys/dimensions, each with their own optional comparator. See examples below.
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/compare
// ES module
<script type="module" src="https://unpkg.com/@thi.ng/compare?module" crossorigin></script>
// UMD
<script src="https://unpkg.com/@thi.ng/compare/lib/index.umd.js" crossorigin></script>
Package sizes (gzipped, pre-treeshake): ESM: 396 bytes / CJS: 471 bytes / UMD: 519 bytes
Several demos in this repo's /examples directory are using this package.
A selection:
Screenshot | Description | Live demo | Source |
---|---|---|---|
Full umbrella repo doc string search w/ paginated results | Demo | Source | |
Triple store query results & sortable table | Demo | Source |
import { ICompare } from "@thi.ng/api";
import { compare } from "@thi.ng/compare";
class Foo implements ICompare<Foo> {
x: number;
constructor(x: number) {
this.x = x;
}
compare(o: Foo) {
return compare(this.x, o.x);
}
}
compare(new Foo(1), new Foo(2));
// -1
Key-based object comparison is supported for 1 - 4 keys / dimensions.
import * as cmp from "@thi.ng/compare";
const src = [
{ id: "charlie", age: 66 },
{ id: "bart", age: 42 },
{ id: "alice", age: 23 },
{ id: "dora", age: 11 },
];
// cluster sort by id -> age (default comparators)
[...src].sort(cmp.compareByKeys2("id", "age"));
// [
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 },
// { id: 'dora', age: 11 }
// ]
// cluster sort by age -> id (default comparators)
[...src].sort(cmp.compareByKeys2("age", "id"));
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
// cluster sort by age -> id
// (custom comparator for `age` key)
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compareNumDesc));
// [
// { id: 'charlie', age: 66 },
// { id: 'bart', age: 42 },
// { id: 'alice', age: 23 },
// { id: 'dora', age: 11 }
// ]
// using `reverse()` comparator for `id`
[...src].sort(cmp.compareByKeys2("age", "id", cmp.compare, cmp.reverse(cmp.compare)));
// [
// { id: 'dora', age: 11 },
// { id: 'alice', age: 23 },
// { id: 'bart', age: 42 },
// { id: 'charlie', age: 66 }
// ]
Karsten Schmidt
If this project contributes to an academic publication, please cite it as:
@misc{thing-compare,
title = "@thi.ng/compare",
author = "Karsten Schmidt",
note = "https://thi.ng/compare",
year = 2016
}
© 2016 - 2021 Karsten Schmidt // Apache Software License 2.0
FAQs
Comparators with support for types implementing the @thi.ng/api/ICompare interface
We found that @thi.ng/compare demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.